/ leetcodesolve / 169. Majority Element.py
swadhinbiswas solve more problems · 1/29/2025
7c666b2
33 lines · 604 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Using BruteForce 
from typing import List
from collections import defaultdict
class Solution:
    #7ms
    def majorityElement(self, nums: List[int]) -> int:
       seen={}
       for number in nums:
        if number not in seen:
           seen[number]=nums.count(number)
        
       return max(seen,key=seen.get)

    #12ms
    def majorityElement(self, nums: List[int]) -> int:
        count_map = defaultdict(int)
        for num in nums:
            count_map[num] += 1
        return max(count_map, key=count_map.get)





nums=[1,1,1,2,2,3]

x=Solution().majorityElement(nums)
print(x)